﻿namespace GameKits.Unity.GuiForms.Forms
{
    using Controls;
    using UnityEngine;

    public class LoggerForm : Form
    {
        private static string logMessages = string.Empty;

        private static bool isCHanged;

        private FixedTextArea logArea;

        protected override void Initialize()
        {
            this.WindowRect = new Rect(Screen.width - 520, 5, 500, 400);
            this.Title = "Logger";
            this.IsDraggable = true;

            this.logArea = new FixedTextArea()
                                {
                                    Position = new Position(5, 30),
                                    Size = new Size(this.WindowRect.width - 10, this.WindowRect.height - 35),
                                    ReadOnly = true
                                };

            var clearButton = new FixedButton
                                {
                                    Text = "Clear",
                                    Position = new Position(this.WindowRect.width - 70, 5),
                                    Size = new Size(60, 20)
                                };

            clearButton.Click += delegate
                                {
                                    Clear();
                                };


            this.Add(this.logArea, clearButton);
        }

        public static void LogMessage(string message)
        {
            if (logMessages == string.Empty)
            {
                logMessages = message;
            }
            else
            {
                logMessages += "\n\r" + message;
            }

            isCHanged = true;

            Debug.Log("ADD MESAGE " + message);
        }

        public static void Clear()
        {
            logMessages = string.Empty;
            isCHanged = true;
        }

        protected override void OnRepaint()
        {
            if (isCHanged)
            {
                this.logArea.Text = logMessages;
                isCHanged = false;
            }
        }
    }
}
